
package com.kugou.game.sdk.adapter;

import com.kugou.game.sdk.entity.MyMessage;
import com.kugou.game.sdk.utils.Rx;
import com.kugou.game.sdk.utils.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MessageBaseAdater extends BaseAdapter {
    private Context context;

    private ArrayList<MyMessage> mArrayList = new ArrayList<MyMessage>();

    private boolean isDeleteMode;

    private DisplayMetrics outMetrics = new DisplayMetrics();

    public MessageBaseAdater(Context context, ArrayList<MyMessage> mArrayList) {
        this.context = context;
        this.mArrayList = mArrayList;
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
    }

    public void refreshData(ArrayList<MyMessage> arrayList) {
        this.mArrayList = arrayList;
    }

    public void addDatas(ArrayList<MyMessage> datas) {
        if (datas != null && datas.size() > 0) {
            this.mArrayList.addAll(datas);
        }
    }

    public void clearDatas() {
        mArrayList.clear();
    }

    public ArrayList<MyMessage> getDatas() {
        return mArrayList;
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public MyMessage getItem(int position) {
        return mArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(Rx.layout.kg_item_message_list, null);
            holder.tvTitle = (TextView) convertView.findViewById(Rx.id.kg_tv_title);
            holder.tvContent = (TextView) convertView.findViewById(Rx.id.kg_tv_content);
            holder.imgDelete = (ImageView) convertView.findViewById(Rx.id.kg_iv_delete);
            holder.imgNoticeType = (ImageView) convertView.findViewById(Rx.id.kg_iv_message_type);
            holder.tvTime = (TextView) convertView.findViewById(Rx.id.kg_tv_time);
            holder.layoutContent = (ViewGroup) convertView.findViewById(Rx.id.kg_layout_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final MyMessage message = mArrayList.get(position);
        holder.layoutContent.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onMessageItemClickListener != null) {
                    onMessageItemClickListener.onMessageItemClick(position, message);
                }
            }
        });
        holder.tvTime.setText((message.getInserttime()).substring(0,
                (message.getInserttime()).length() - 3));
        holder.tvTitle.setText(message.getTitle() + "  ");

        String content = message.getContent();
        if (!TextUtils.isEmpty(content)) {
            holder.tvContent.setText(Html.fromHtml(content));
        } else {
            holder.tvContent.setText("");
        }

        holder.tvTime.setText(""
                + (message.getInserttime()).substring(0, (message.getInserttime()).length() - 3));
        if (message.getClassid() == 1) {
            holder.imgNoticeType.setImageResource(Rx.drawable.kg_icon_system_notice);
        } else {
            // holder.imgNoticeType.setImageResource(Rx.drawable.kg_icon_new_server_notice);
            holder.imgNoticeType.setImageResource(Rx.drawable.kg_icon_system_notice);
        }
        holder.imgDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onDeleteClickListener != null) {
                    onDeleteClickListener.onDelete(position, message);
                }
            }
        });

        if (isDeleteMode()) {
            holder.imgDelete.setVisibility(View.VISIBLE);
        } else {
            holder.imgDelete.setVisibility(View.GONE);
        }
        if (message.getIsread() == 0) {
            Drawable drawable = context.getResources().getDrawable(Rx.drawable.kg_icon_unread);
            drawable.setBounds(0, 0, (int) (15 * outMetrics.density),
                    (int) (15 * outMetrics.density));
            holder.tvTitle.setCompoundDrawables(null, null, drawable, null);
        } else {
            holder.tvTitle.setCompoundDrawables(null, null, null, null);
        }
        return convertView;
    }

    public boolean isDeleteMode() {
        return isDeleteMode;
    }

    public void setDeleteMode(boolean isDeleteMode) {
        this.isDeleteMode = isDeleteMode;
    }

    class ViewHolder {

        public TextView tvTitle;

        public TextView tvContent;

        public TextView tvTime;

        public ImageView imgDelete;

        public ImageView imgNoticeType;

        public ViewGroup layoutContent;

    }

    private OnDeleteClickListener onDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        this.onDeleteClickListener = listener;
    }

    public interface OnDeleteClickListener {
        public void onDelete(int position, MyMessage myMessage);
    }

    private OnMessageItemClickListener onMessageItemClickListener;

    public void setOnMessageItemClickListener(OnMessageItemClickListener onMessageItemClickListener) {
        this.onMessageItemClickListener = onMessageItemClickListener;
    }

    public interface OnMessageItemClickListener {
        public void onMessageItemClick(int position, MyMessage myMessage);
    }
}
